<template>
  <el-card style="max-width: 480px;margin: 200px auto">
    <h1>欢迎登录学生管理系统</h1>
    <el-form :model="form" label-width="auto" style="max-width: 480px">
      <el-form-item label="账号">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登 录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: "LoginView",
  data(){
    return {
      form : {username:'',password:''}   //username=xxx&password=xx
    }
  },
  methods:{
    login(){
      //调用登录后台方法
      this.$axios.post('/login',this.$qs.stringify(this.form))
          .then(result => {
            console.log('login',result.data)
            //判断是否连接成功
            if(result.data.code === 200 ){
              //判断用户是否存在
              if(result.data.data){
                //跳转到主界面
                ElMessage.success('登录成功')
                //保存用户信息
                localStorage.setItem('user',JSON.stringify(result.data.data))
                this.$router.push({path:'/index'})
              }else{
                ElMessage.error('账号或密码错误')
              }
            }
          })
    }
  }
}
</script>

<style scoped>

</style>